import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import java.util.List;
import java.util.Scanner;

/**
 * @author acproject@qq.com
 * @date 2022/3/15
 */
public class GremlinTest {
    public static String getPerson(GraphTraversalSource g) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the first name for the person to find: ");
        String name = keyboard.nextLine();

        List peoperties = g.V()
                .has("person", "first_name", name)
                .valueMap("first_name").toList();
        return peoperties.toString();
    }

    public static String addPerson(GraphTraversalSource g) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the name for the person to add: ");
        String name = keyboard.nextLine();

        Vertex newVertex = g.addV("person")
                .property("first_name", name).next();
        return newVertex.toString();
    }

    public static String addFriendsEdge(GraphTraversalSource g) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the name for person at the edge start: ");
        String fromName = keyboard.nextLine();
        System.out.println("Enter the name for person at the edge end: ");
        String toName = keyboard.nextLine();

        Edge newEdge = g.addE("friends")
                .from(
                        __.V().has("person", "first_name", fromName)
                ).to(
                        __.V().has("person", "first_name", toName)
                ).next();

        return newEdge.toString();
    }

    public static String updatePerson(GraphTraversalSource g) {
        Scanner keyborad = new Scanner(System.in);
        System.out.println("Enter the name for person to update: ");
        String name =keyborad.nextLine();
        System.out.println("Enter the new name for the person:  ");
        String newName = keyborad.nextLine();

        Vertex vertex = g.V().has("person", "first_name", name)
                .property("first_name", newName) // 这里设置一个新的名字
                .next();
        return vertex.toString();
    }

    public static String deletePerson(GraphTraversalSource g) {
        Scanner keyborad = new Scanner(System.in);
        System.out.println("Enter the name for person to delete: ");
        String name = keyborad.nextLine();
        Long vertexCount = g.V()
                .has("person", "first_name", name)
                .sideEffect(__.drop())
                .count().next();
        return vertexCount.toString();
    }
}
